package Tools;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import org.apache.http.util.EncodingUtils;
import com.fs.zise.ZiSeActivity;

import android.R.integer;
import android.content.res.AssetManager;

public class ToolUtil {
	public static Random  rand= new Random();

	
	/**
	 * 
	 * @param res 源数组
	 * @param begin 从第几位开始插入
	 * @param data  需要插入的数组元素
	 * @return
	 */
	public static short[] arrayInsert(short[] res, int begin, short[] data) {
		int length = data.length;
		short[] buffer = new short[res.length+length];
		System.arraycopy(res, 0, buffer, 0, begin);
		System.arraycopy(data, 0, buffer, begin, data.length);
		System.arraycopy(res, begin, buffer, begin+length, res.length-begin);
		
		for (int i = 0; i < buffer.length; i++) {
			System.out.println("buffer="+buffer[i]);
		}
		return buffer;
	} 
	
	/**
	 * �������顣
	 * 
	 * @param array
	 */
	public static final void obfArray(int[] array, int times) {
		int ai, tmp;
		for (int i = times - 1; i > 0; i--) {
			if (i >= times - 11) {
				ai = getRandInt(0, i + 1);
				tmp = array[i];
				array[i] = array[ai];
				array[ai] = tmp;
			} else if (array[i] == array[i + 1] || array[i] == array[i + 10]) {
				ai = getRandInt(0, i + 1);
				tmp = array[i];
				array[i] = array[ai];
				array[ai] = tmp;
			}
		}
	}

	/**
	 * 生成[rg1,rg2)范围内的随机数。
	 * 
	 * @param rg1
	 * @param rg2
	 *            rg2 > rg1
	 * @return 生成的随机数。 rg1 > rg2时，返回 0。
	 */
	public static final int getRandInt(int rg1, int rg2) {
		int ret;
		if (rg1 > rg2) {
			/* ����������� */
			ret = 0;
		} else if (rg1 == rg2) {
			ret = rg1;
		} else {
			ret = (rand.nextInt() & 0x7fffffff) % (rg2 - rg1) + rg1;
		}
		return ret;
	}
	
	public static void traceArray(int[][] a_array)
	{
		for (int i = 0; i < a_array.length; i++) {
			for (int j = 0; j < a_array[i].length; j++) {
				System.out.print(""+a_array[i][j]+" ");
			}
			System.out.println("");
		}
	}
	
	  public static boolean ArrayAllSameElements(int [] array)
	   {
		   int firstElement = array[0];
		   int ff = 1;
		   for (int i = 0; i < array.length; i++) {
			if(array[i] != firstElement)
				ff = 0;
		   }
		   
		   if(ff == 1)
		   {
			   System.out.println("一样的元素");
			   return true;
		   }
		   else if(ff == 0)
		   {
			   System.out.println("不一样的元素");
			   return false;
		   }
		   else
		   {
			   System.out.println("其他的情况");
			   return false;
		   }
	   }
	   
	
	  public static boolean ArrayAllIncreasedOrDecreased(int[] array_dif)
	   {
		   int ff = 1;
		   int f = 1;
		   boolean increased = false;
		   boolean descreased = false;
		   
		   for (int i = 0; i < array_dif.length-1; i++) {
			for (int j = i+1; j < array_dif.length; j++) {
				if(array_dif[i] == array_dif[j])
				{
					System.out.println("有相同元素");
					return false;
				}
			}
		}
		   
		   if(array_dif.length == 2)
		   {
			   if(array_dif[0]-array_dif[array_dif.length-1]==1)
			   {
				   System.out.println("2个元素降序");
				   return true;
			   }
			   else if(array_dif[0]-array_dif[array_dif.length-1]== -1)
			   {
				   System.out.println("2个元素升序");
				   return true;
			   }
			   else {
				System.out.println("2个元素乱序");
				return false;
			}
		   }
				 
		   
		   for (int i = 0; i < array_dif.length-1; i++) {
			   int delta = array_dif[1] - array_dif[0];
				   if(array_dif[i+1]>array_dif[i])
				   {
					  increased = true;
					   if(array_dif[i+1] - array_dif[i] != delta )
				   		{
						   ff = 0;
						   f = 0;
				   		}
				   }
				     if(array_dif[i+1]<array_dif[i])
				   {
					   descreased = true;
					   if(array_dif[i+1] - array_dif[i] != delta )
					   {
						   f = 0;
						   ff = 0;
					   }
				   }
		   }
		   if(ff == 1 && increased )
		   {
			   System.out.println("一行升序");
			   return true;
		   }	
		   if(f == 1 && descreased)
		   {
			   System.out.println("一行降序");
			   return true;
		   }	  
		   else
		   {  
			   System.out.println("乱序");
			   return false;
		   }
	   }
	  
	  /**
	   * 
	   * 交换一数组中 一行两不同列的数组元素
	   * @param array
	   * @param col1
	   * @param col2
	   * @param sameRow
	   * @return
	   */
		public static int[][] changeC1Row(int[][] array,int col1,int col2,int sameRow)
		{
				 int temp  = array[sameRow][col1];
				 array[sameRow][col1] = array[sameRow][col2];
				 array[sameRow][col2] = temp;
			return array;
		}
		
		 /**
		   * 
		   * 交换一数组中 一列两不同行的数组元素
		   * @param array
		   * @param col1
		   * @param col2
		   * @param sameRow
		   * @return
		   */
			public static int[][] changeR1Col(int[][] array,int row1,int row2,int sameCol)
			{
					 int temp  = array[row1][sameCol];
					 array[row1][sameCol] = array[row2][sameCol];
					 array[row2][sameCol] = temp;
				return array;
			}
	
			public static String readDataFromTXTFile(String filePath)
			{
				if(filePath == "")
					return "";
				
				String resString = "";
				try {
					AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
					InputStream is = assetManager.open(filePath);
//					BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
//					
//					while (bufferedReader.read) {
//						
//					}
					
					byte[] buffer = new byte[1024];
					is.read(buffer);
					resString = EncodingUtils.getString(buffer, "UTF-8");
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				return resString;
			}
			
}
